package com.hospital.controller;



import com.hospital.dao.impl.AdminDao;
import com.hospital.model.Admin;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
/**
 * 处理管理员登录请求，验证用户名和密码，并重定向到管理员主页
 */
@WebServlet("/adminLogin")
public class AdminLoginServlet extends HttpServlet {
    /**
     * 管理员数据访问对象，用于处理管理员登录验证
     */
    private AdminDao adminDao = new AdminDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Admin admin = adminDao.login(username, password);

        if (admin != null) {
            // 登录成功
            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);
            session.setAttribute("userType", "admin");
            
            // 重定向到管理员主页
            response.sendRedirect("pages/admin/index.jsp");
        } else {
            // 登录失败
            request.setAttribute("error", "用户名或密码错误");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect("login.jsp");
    }
} 